package plsqlfs.sql;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;

public class RichCallableStatement {

	private final CallableStatement delegate;

	public RichCallableStatement(CallableStatement delegate) {
		this.delegate = delegate;
	}

	public static RichCallableStatement wrap(CallableStatement delegate) {
		return new RichCallableStatement(delegate);
	}

	public class UpdateResult {

		private final int count;

		UpdateResult(int count) {
			this.count = count;
		}

		public int getCount() {
			return count;
		}

		public String getString(int parameterIndex) throws SQLException {
			return delegate.getString(parameterIndex);
		}

	}

	public RichCallableStatement registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
		delegate.registerOutParameter(parameterIndex, sqlType);
		return this;
	}

	public RichCallableStatement registerOutVarcharParameter(int parameterIndex) throws SQLException {
		delegate.registerOutParameter(parameterIndex, Types.VARCHAR);
		return this;
	}

	public UpdateResult executeUpdate() throws SQLException {
		return new UpdateResult( delegate.executeUpdate() );
	}

	public RichCallableStatement set(int parameterIndex, int x) throws SQLException {
		delegate.setInt(parameterIndex, x);
		return this;
	}

	public RichCallableStatement set(int parameterIndex, Integer x) throws SQLException {
		if (x == null) {
			delegate.setNull(parameterIndex, Types.INTEGER);
		} else {
			delegate.setInt(parameterIndex, x.intValue());
		}
		return this;
	}

	public RichCallableStatement set(String parameterName, int x) throws SQLException {
		delegate.setInt(parameterName, x);
		return this;
	}

	public RichCallableStatement set(String parameterName, Integer x) throws SQLException {
		if (x == null) {
			delegate.setNull(parameterName, Types.INTEGER);
		} else {
			delegate.setInt(parameterName, x.intValue());
		}
		return this;
	}

	public RichCallableStatement set(int parameterIndex, long x) throws SQLException {
		delegate.setLong(parameterIndex, x);
		return this;
	}

	public RichCallableStatement set(int parameterIndex, Long x) throws SQLException {
		if (x == null) {
			delegate.setNull(parameterIndex, Types.INTEGER);
		} else {
			delegate.setLong(parameterIndex, x.longValue());
		}
		return this;
	}

	public RichCallableStatement set(String parameterName, long x) throws SQLException {
		delegate.setLong(parameterName, x);
		return this;
	}

	public RichCallableStatement set(String parameterName, Long x) throws SQLException {
		if (x == null) {
			delegate.setNull(parameterName, Types.INTEGER);
		} else {
			delegate.setLong(parameterName, x.longValue());
		}
		return this;
	}

	public RichCallableStatement set(int parameterIndex, String x) throws SQLException {
		delegate.setString(parameterIndex, x);
		return this;
	}

}
